<template>
  <div class="app-container customer-div">
    <el-form :model="supplierForm" size="mini" label-width="130px" :rules="rules" ref="supplierForm" style="width:1000px">
      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"></svg-icon>
        <span class="font-small">供应商信息</span>
      </div>
      <el-card shadow="never" style="margin-top:20px">
        <el-row>
          <el-col :span="14">
            <el-form-item label="上传营业执照：">
              <el-upload
                accept="image/png,image/jpg,image/jpeg"
                drag
                action="https://upload.qiniup.com"
                :data="uploadToken"
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                  <i class="el-icon-upload" v-if="supplierForm.businessLicense == ''"></i>
                  <div class="el-upload__text"  v-if="supplierForm.businessLicense == ''">将文件拖到此处，或<em>点击上传</em></div>
                  <img :src="supplierForm.businessLicense" v-else style="width:100%;height:100%;object-fit: contain;"/>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="统一社会信用代码：">
              <!-- @blur="seachRepeat" -->
              <el-input v-model.trim="supplierForm.businessCode"  clearable style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="供应商名称：">
              <el-input v-model="companyInfo.companyName" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="成立日期：">
              <el-input v-model="companyInfo.regTime"  style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="法人：">
               <el-input v-model="companyInfo.legalPerson"  style="width:200px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="注册资金：">
              <el-input v-model="companyInfo.regMoney"  style="width:200px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="实缴资金：">
              <el-input v-model="companyInfo.actualMoney"  style="width:200px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="注册地址：">
              <el-input type="textarea" v-model="companyInfo.companyAddress"  style="width:270px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="实际地址：">
              <el-input type="textarea" v-model="supplierForm.actualAddress" :autosize="{ minRows: 2, maxRows: 4}" :rows="2"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"></svg-icon>
        <span class="font-small">组织人员(新建时至少填写一条)</span>
      </div>
      <el-card shadow="never" style="margin-top:20px">
        <el-table :data="supplierForm.contactList" border style="width: 100%;margin:20px 0">
          <el-table-column
            align="center"
            label="联系人"
            width="150">
            <template slot-scope="scope">
              <el-form-item :prop="`contactList.${scope.$index}.contactName`" :rules="rules.contactName" label-width="0px">
                <el-input v-model="scope.row.contactName" clearable style="width:100%" ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="手机号"
            width="150"
            align="center">
            <template slot-scope="scope">
              <el-form-item :prop="`contactList.${scope.$index}.contactMobile`" :rules="rules.contactMobile" label-width="0px">
                <el-input v-model="scope.row.contactMobile" clearable style="width:100%"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="职务"
            width="150"
            align="center">
            <template slot-scope="scope">
              <el-form-item label-width="0px">
                <el-select v-model="scope.row.contactDuty" clearable style="width:100%">
                  <el-option v-for="(item,index) in supplierDutyList" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  label="是否为主联系人" width="150" align="center">
            <template slot-scope="scope">
              <el-form-item label-width="0px">
                  <el-radio-group v-model="scope.row.isMainContact">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                  </el-radio-group>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  label="是否有采购决定权" width="150" align="center">
            <template slot-scope="scope">
              <el-form-item label-width="0px">
                <el-radio-group v-model="scope.row.isBuyPermission">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="0">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            width="200"
            align="center"
            label="产品线">
            <template slot-scope="scope">
              <el-form-item label-width="0px">
                <el-select v-model="scope.row.goodsLineIds" filterable multiple>
                  <el-option v-for="(item,index) in goodsLineList" :key="index" :label="item.cddValue" :value="item.cddId"></el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  label="操作" width="100" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button v-if="supplierForm.contactList.length>1" type="text" @click="delContact(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" size="small" @click="addContact">新增人员</el-button>
      </el-card>

      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"></svg-icon>
        <span class="font-small">供应商等级(必填)</span>
      </div>
      <el-card shadow="never" style="margin-top:20px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商类型：" prop="supplierType">
              <el-select v-model="supplierForm.supplierType" clearable @change="sumPrePayAmount">
                <el-option v-for="(item,index) in supplierTypeList" :key="index" :value="item" :label="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商性质：" prop="supplierQuantity">
              <el-select v-model="supplierForm.supplierQuantity" clearable style="width: 240px;" @change="sumPrePayAmount">
                <el-option v-for="(item,index) in supplierQuantityList" :key="index" :value="item" :label="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用度：" prop="supplierCredit">
              <el-select v-model="supplierForm.supplierCredit" clearable style="width: 240px;" @change="sumPrePayAmount">
                <el-option v-for="(item,index) in supplierCreditList" :key="index" :value="item" :label="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出货量&占有率：" prop="outQuantity">
              <el-select  v-model="supplierForm.outQuantity" clearable style="width: 300px;" @change="sumPrePayAmount">
                <el-option v-for="(item,index) in outQuantityList" :key="index" :value="item" :label="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="supplierForm.supplierType=='工厂'|| supplierForm.supplierType == '聚合工厂'" label="是否意向获得代理授权：" label-width="160px" prop="isIntentionAgent">
              <el-radio-group v-model="supplierForm.isIntentionAgent">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="计算预付款额度：" >
                {{prePayAmount}}
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="投产日期：" prop="dateType">-->
<!--                <el-radio-group v-model="supplierForm.dateType" >-->
<!--                  <el-radio :label="0">无投产日期</el-radio>-->
<!--                  <el-radio :label="1">日期</el-radio>-->
<!--                </el-radio-group>-->
<!--                <el-form-item label="" prop="productionDate" label-width="0px" v-if="supplierForm.dateType == 1">-->
<!--                  <el-date-picker-->
<!--                    format="yyyy 年 MM 月 dd 日"-->
<!--                    value-format="yyyy-MM-dd"-->
<!--                    v-model="supplierForm.productionDate"-->
<!--                    type="date"-->
<!--                    placeholder="选择日期">-->
<!--                  </el-date-picker>-->
<!--                </el-form-item>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>
      </el-card>

      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"></svg-icon>
        <span class="font-small">业务经营(必填)</span>
      </div>
      <el-card shadow="never" style="margin-top:20px">
        <el-row>
           <el-col :span="8">
             <el-form-item label="装置工艺" label-width="100px">
               <el-input v-model="supplierForm.equipmentProcess"></el-input>
             </el-form-item>
           </el-col>
          <el-col :span="16">


          </el-col>
        </el-row>
        <div v-for="(dataItem ,index) in supplierForm.goodsList" style="width: 100%;margin:20px 0;position: relative" >
          <div style="position: absolute;right:-5px;top:-5px;cursor: pointer" @click="delGoodsList(index)"><i class="el-icon-error" style="font-size: 18px"></i></div>
          <table border="0" cellspacing="0" cellpadding="0" class="supplier-table">
             <tr>
                <th>产品线</th>
                <th>牌号</th>
                <th>产能（吨/月）</th>
                <th>库存（吨/月）</th>
                <th>产业链</th>
             </tr>
            <tr>
              <td>
                <el-form-item label-width="0px" :prop="`goodsList.${index}.goodsLineId`" :rules="rules.goodsLineId">
                  <el-select v-model="dataItem.goodsLineId" filterable clearable>
                    <el-option v-for="(item,index) in goodsLineList" :key="index" :label="item.cddValue" :value="item.cddId"></el-option>
                  </el-select>
                </el-form-item>
              </td>
              <td>
                <el-form-item :prop="`goodsList.${index}.goodsKingId`" :rules="rules.goodsKingId" label-width="0px">
                  <el-select v-model="dataItem.goodsKingId" filterable placeholder="请选择" clearable>
                    <el-option
                      v-for="item in goodsList"
                      :key="item.kingId"
                      :label="item.goodsSn"
                      :value="item.kingId">
                    </el-option>
                  </el-select>

<!--                  <el-input v-model="dataItem.goodsSn" clearable style="width:100%"></el-input>-->
                </el-form-item>
              </td>
              <td>
                <el-form-item label-width="0px">
                  <el-input-number v-model="dataItem.productionQuantity" :controls="false" :min="0" style="width:100%"></el-input-number>
                </el-form-item>
              </td>
              <td>
                <el-form-item label-width="0px">
                  <el-input-number v-model="dataItem.supplierStock" :controls="false" :min="0" style="width:100%"></el-input-number>
                </el-form-item>
              </td>
              <td>
                <el-form-item label-width="0px">
                  <el-input v-model="dataItem.industrialChain" clearable style="width:100%"></el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>对标产品</th>
              <th>对标工厂</th>
              <th>销售模式</th>
              <th>投产日期</th>
              <th>负责人</th>
            </tr>
            <tr>
              <td>
                <el-form-item label-width="0px">
                  <el-input v-model="dataItem.benchmarkingProduct" clearable style="width:100%"></el-input>
                </el-form-item>
              </td>
              <td>
                <el-form-item  label-width="0px">
                  <el-input v-model="dataItem.benchmarkingFactory" clearable style="width:100%"></el-input>
                </el-form-item>
              </td>
              <td>
                <el-form-item label-width="0px">
                    <el-select v-model="dataItem.saleModel">
                      <el-option value="竞拍" label="竞拍"></el-option>
                      <el-option value="大单" label="大单"></el-option>
                      <el-option value="预售" label="预售"></el-option>
                      <el-option value="底仓" label="底仓"></el-option>
                      <el-option value="产销平衡" label="产销平衡"></el-option>
                    </el-select>
                </el-form-item>
              </td>
              <td>
                <el-form-item label-width="0px" :prop="`goodsList.${index}.dateType`" :rules="rules.dateType">
                  <el-radio-group v-model="dataItem.dateType" >
                    <el-radio :label="0">无投产日期</el-radio>
                    <el-radio :label="1">日期</el-radio>
                  </el-radio-group>
                  <el-form-item label-width="0px" v-if="dataItem.dateType == 1" :prop="`goodsList.${index}.productionDate`" :rules="rules.productionDate">
                    <el-date-picker
                      format="yyyy 年 MM 月 dd 日"
                      value-format="yyyy-MM-dd"
                      v-model="dataItem.productionDate"
                      type="date"
                      placeholder="选择日期">
                    </el-date-picker>
                  </el-form-item>
                </el-form-item>
              </td>
              <td>
                  <el-form-item label-width="0px">
                    <el-select v-model="dataItem.purchaseUserId" filterable clearable>
                      <el-option v-for="(item,index) in userOptionList" :key="index" :label="item.userName" :value="item.userId"></el-option>
                    </el-select>
                  </el-form-item>
              </td>
            </tr>
          </table>
<!--          <el-table :data="dataItem" border style="width: 100%;margin:20px 0">-->
<!--            <el-table-column-->
<!--              width="150"-->
<!--              align="center"-->
<!--              label="产品线">-->
<!--              <template slot-scope="scope">-->
<!--                <el-form-item label-width="0px" :prop="`goodsList.${index}.goodsLineId`" :rules="rules.goodsLineId">-->
<!--                  <el-select v-model="scope.row.goodsLineId" filterable clearable>-->
<!--                    <el-option v-for="(item,index) in goodsLineList" :key="index" :label="item.cddValue" :value="item.cddId"></el-option>-->
<!--                  </el-select>-->
<!--                </el-form-item>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--              align="center"-->
<!--              label="牌号"-->
<!--              width="230">-->
<!--              <template slot-scope="scope">-->
<!--                <el-form-item :prop="`goodsList.${scope.$index}.goodsSn`" :rules="rules.goodsSn" label-width="0px">-->
<!--                  <el-input v-model="scope.row.goodsSn" clearable style="width:100%"></el-input>-->
<!--                  &lt;!&ndash; <el-select-->
<!--                    v-model="scope.row.goodsSn"-->
<!--                    filterable-->
<!--                    remote-->
<!--                    clearable-->
<!--                    :remote-method="getOrderGoodsList"-->
<!--                    :loading="searchLoading"-->
<!--                    placeholder="请输入关键词">-->
<!--                    <el-option v-for="item in goodsSnList" :key="item.kingId" :label="item.goodsSn" :value="item.kingId"></el-option>-->
<!--                  </el-select> &ndash;&gt;-->
<!--                </el-form-item>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--              label="产能(吨/每月)"-->
<!--              width="150"-->
<!--              align="center">-->
<!--              <template slot-scope="scope">-->
<!--                <el-form-item label-width="0px">-->
<!--                  <el-input-number v-model="scope.row.productionQuantity" :controls="false" :min="0" style="width:100%"></el-input-number>-->
<!--                </el-form-item>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--              label="库存(吨/月)"-->
<!--              width="150"-->
<!--              align="center">-->
<!--              <template slot-scope="scope">-->
<!--                <el-form-item label-width="0px">-->
<!--                  <el-input-number v-model="scope.row.supplierStock" :controls="false" :min="0" style="width:100%"></el-input-number>-->
<!--                </el-form-item>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--              label="产业链"-->
<!--              width="150"-->
<!--              align="center">-->
<!--              <template slot-scope="scope">-->
<!--                <el-form-item label-width="0px">-->
<!--                  <el-input v-model="scope.row.industrialChain" clearable style="width:100%"></el-input>-->
<!--                </el-form-item>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--              label="负责人"-->
<!--              width="150"-->
<!--              align="center">-->
<!--              <template slot-scope="scope">-->
<!--                <el-form-item label-width="0px">-->
<!--                  <el-select v-model="scope.row.purchaseUserId" filterable clearable>-->
<!--                    <el-option v-for="(item,index) in userOptionList" :key="index" :label="item.userName" :value="item.userId"></el-option>-->
<!--                  </el-select>-->
<!--                </el-form-item>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column  label="操作" width="100" align="center" fixed="right">-->
<!--              <template slot-scope="scope">-->
<!--                <el-button v-if="supplierForm.goodsList.length>1" type="text" @click="delGoodsList(scope.$index)">删除</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--          </el-table>-->
        </div>

        <el-button type="primary" size="small" @click="addGoodsList">新增产线</el-button>
      </el-card>
    </el-form>
    <div style="margin-top:20px;text-align: center;width:1000px">
      <el-button size="small" @click="toBalck">返回</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="btnLoading" size="small">提交审核</el-button>
    </div>
  </div>
</template>
<script>
import {
  getOrderUsersList,
  getQiniuToken,
  getCustomerSetting,addMaterialConfig,orderGoodsList,computePreAmount
} from '@/api/partner';
import {
  getSupplierInfo,
  supplierEdit,
  licenceAPI,
  supplierConstantAPI,
  supplierContactList,
  supplierGoodsList,
} from '@/api/company';
let md5 = require('md5');
import { formatDate } from '@/utils/date';
import { validatePhone } from '@/utils/validate';
import store from '@/store';
import { mapGetters } from 'vuex';
const defaultSupplierForm = {
  supplierId: '',
  businessCode: '',//统一信用码
  businessLicense: '',//营业执照
  supplierType: '',//供应商类型
  supplierQuantity: '',//供应商性质
  isIntentionAgent: '',//是否意向获得代理权
  supplierCredit: '',//供应商信用
  outQuantity: '',//出货量&占用率
  actualAddress: '',//实际地址
  contactList:[],//组织人员
  goodsList:[],//业务经营
  equipmentProcess:'',//装置工艺
};
const defaultCompanyInfo = {
  actualMoney: '',
  companyAddress: '',
  companyName: '',
  legalPerson: '',
  regMoney: '',
  regTime:'',
};
const defeaultContactList = {//新增组织人员发
  contactId: '',//编辑ID
  contactName:'',//联系人
  contactMobile: '',//手机号
  contactDuty:'',//职务
  isMainContact:'',//是否主联系人
  isBuyPermission: '',//是否有采购决定权
  goodsLineIds:[],//产品线
};
const defeaultGoodsList = {//新增产线
  goodsKingId: '',//牌号ID
  goodsLineId:'',//产品线ID
  productionQuantity:'',//产能
  supplierStock: '',//库存(吨/月)
  industrialChain:'',//产业链
  purchaseUserId: '',//采购负责人
  benchmarkingProduct:'',// 对标产品
  benchmarkingFactory:'',//  对标工厂
  saleModel:'',//   销售模式
  productionDate:'',//投产日期
  dateType:'',//投产日期类型

};
export default {
  name: "addSupplier",
  data() {
    const isvalidatePhone = (rule, value, callback) => {
      if (!validatePhone(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    };
    return {
      type: this.$route.query.type,
      userOptionList: [],
      goodsLineList: [],
      roles: store.getters.roles ? store.getters.roles : [],
      supplierForm: JSON.parse(JSON.stringify(defaultSupplierForm)),
      companyInfo: JSON.parse(JSON.stringify(defaultCompanyInfo)),
      supplierTypeList:[],//供应商类型
      outQuantityList: [],//出货量
      supplierCreditList: [],//供应商信用度
      supplierQuantityList: [],//供应商性质
      supplierDutyList:[],//职务列表
      btnLoading: false,
      uploadToken: {
        token: '',
      },
      imgDomain: '',
      rules: {
        businessLicense: [{ required: true, message: '请上传营业执照', trigger: 'blur' }],
        contactName:[{required:true,message:'请输入联系人姓名',trigger:'blur'}],
        contactMobile: [
          { required: true, message: '请输入联系人手机号', trigger: 'blur' },
          { validator: isvalidatePhone, trigger: 'blur' }
        ],
        isIntentionAgent: [{ required: true, message: '请选择', trigger: 'change' }],
        supplierType: [{ required: true, message: '请选择供应商类型', trigger: 'change' }],
        supplierQuantity: [{ required: true, message: '请选择供应商性质', trigger: 'change' }],
        supplierCredit: [{ required: true, message: '请选择信用度', trigger: 'change' }],
        outQuantity: [{ required: true, message: '请选择出货量', trigger: 'change' }],
        goodsLineId: [{ required: true, message: '请选择产品线', trigger: 'change' }],
        goodsKingId: [{ required: true, message: '请选择牌号', trigger: 'change' }],
        purchaseUserId: [{ required: true, message: '请选择负责人', trigger: 'change' }],
        dateType:[{ required: true, message: '请选择投产日期', trigger: 'change' }],
        productionDate:[{ required: true, message: '请选择投产日期', trigger: 'change' }],
      },
      goodsList:[],
      prePayAmount:''
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  created() {
    this.showCon();
    this.getUploadToken();
    this.getUserOptionList();
    this.getCustomerSettingList();
    this.getSupplierConstant();
    this.getGoodsList()
  },
  filters: {

  },
  methods: {
    sumPrePayAmount(){
      let data = {}
      data.supplierType = this.supplierForm.supplierType;
      data.supplierQuantity = this.supplierForm.supplierQuantity;
      data.supplierCredit = this.supplierForm.supplierCredit;
      data.outQuantity = this.supplierForm.outQuantity
      computePreAmount(data).then((res)=>{
        this.prePayAmount = res.prePayAmount
      })
    },
    getGoodsList(){
      orderGoodsList().then((res)=>{
        this.goodsList = res.list
      })
    },
    // 新增产线
    addGoodsList(){
      let c = JSON.parse(JSON.stringify(defeaultGoodsList))
      c.purchaseUserId = this.userId
      this.supplierForm.goodsList.push(c)
    },
    // 删除产线
    delGoodsList(index) {
      this.supplierForm.goodsList.splice(index,1)
    },
    //新增组织人员
    addContact(){
      this.supplierForm.contactList.push(JSON.parse(JSON.stringify(defeaultContactList)))
    },
    //删除组织人员
    delContact(index){
      this.supplierForm.contactList.splice(index, 1)
    },
    //获取客户配置
    getCustomerSettingList(){
      getCustomerSetting().then((res) =>{
        res.forEach((el) => {
          if (el.cdtCode == 'goods_line') {//产品线
            this.goodsLineList = el.data
          }
        });
      })
    },
    getSupplierConstant(){
      supplierConstantAPI().then((res) =>{
        this.supplierTypeList = res.supplierType;
        this.outQuantityList = res.outQuantity;
        this.supplierCreditList = res.supplierCredit;
        this.supplierQuantityList = res.supplierQuantity;
        this.supplierDutyList = res.supplierDuty;
      }).catch(()=>{})
    },

    //上传图片
    handleAvatarSuccess(res){
      this.supplierForm.businessLicense = this.imgDomain + res.key;
      let encryption =  md5(this.supplierForm.businessLicense + 'crm..!');
      licenceAPI({
        licensePicUrl: this.supplierForm.businessLicense,
        encryption:encryption,
      }).then((res) => {
        this.supplierForm.businessCode = res.creditCode;
        for (let key in defaultCompanyInfo) {
          this.companyInfo[key] = res[key] ? res[key] : defaultCompanyInfo[key];
        }
      }).catch(()=>{})
    },

    //获取七牛TOKEN
    getUploadToken(){
      getQiniuToken().then((res) => {
        this.uploadToken.token = res.token;
        this.imgDomain = res.domain
      })
    },

    //返回
    toBalck(){
      this.$router.go(-1)
    },

    //显示新建编辑客户详情
    showCon(){
      if (this.type == 'add'){
        this.supplierForm = JSON.parse(JSON.stringify(defaultSupplierForm));
      } else if (this.type == 'edit') {
        let supplierId = this.$route.query.id;
        getSupplierInfo({supplierId}).then(res => {
          for (let key in defaultSupplierForm) {
            this.supplierForm[key] = res[key] ? res[key] : defaultSupplierForm[key];
          }
          this.companyInfo.companyName = res.supplierName;
          this.companyInfo.regTime = res.regDate;
          this.companyInfo.legalPerson = res.legalPerson;
          this.companyInfo.regMoney = res.regMoney;
          this.companyInfo.actualMoney = res.actualMoney;
          this.companyInfo.companyAddress = res.addressDetail;
          this.supplierForm.isIntentionAgent = res.isIntentionAgent;
          this.getSupplierContactList(supplierId);
          this.getSupplierGoodsList(supplierId);
          this.sumPrePayAmount()
        })

      }
    },

    // 获取组织人员列表
    getSupplierContactList(supplierId){
      supplierContactList({supplierId}).then((res) => {
        this.supplierForm.contactList = res.list;
        res.list.forEach((item, index) => {
          if (item.goodsLineId) {
            this.supplierForm.contactList[index].goodsLineIds =item.goodsLineId.split(',').map(Number);
          }
        })
      })
    },

    // 获取业务经营列表
    getSupplierGoodsList(supplierId){
      supplierGoodsList({supplierId}).then((res) => {

        res.list.forEach((el)=>{
          for (let key in defeaultGoodsList) {
            el[key] = el[key] || el[key] == 0 ? el[key] : defeaultGoodsList[key];
          }
          if(el.productionDate=== ''){
            el.dateType = ''
          }else{
            el.dateType = el.productionDate != 0 ? 1 : 0
          }
        })
        this.supplierForm.goodsList = res.list;
      })
    },

    //提交新建编辑
    handleConfirm(){
      if (this.supplierForm.contactList.length == 0) {
        this.$message.error('请添加组织人员');
        return false
      } else if (this.supplierForm.goodsList.length == 0) {
        this.$message.error('请添加业务经营');
        return false
      }
      this.$refs.supplierForm.validate((valid) => {
        let data = JSON.parse(JSON.stringify(this.supplierForm));
        if (valid) {
          data.contactList.forEach((item,index) => {
            data.contactList[index].goodsLineIds= item.goodsLineIds.join(',')
          })
          data.goodsList.forEach((el)=>{
            el.productionDate = el.dateType == 0 ? 0 : el.productionDate
          })
          data.isIntentionAgent = data.supplierType == '工厂' || data.supplierType == '聚合工厂' ? data.isIntentionAgent : '';
          this.btnLoading = true;
          supplierEdit(data).then(() => {
            this.btnLoading = false;
            this.$message.success('操作成功');
            this.toBalck()
          }).catch(() => {
            this.btnLoading = false;
          })
        }
      })
    },

    //获取采购负责人下拉选项
    getUserOptionList() {
      getOrderUsersList({departmentType:"purchase"}).then((res) => {
        this.userOptionList = res;
      })
    },

    // 获取牌号列表
    // getOrderGoodsList(query) {
    //   this.searchLoading = true;
    //   orderGoodsList({
    //     keywords:query
    //   }).then((res) => {
    //     this.searchLoading = false;
    //     this.goodsSnList = res.list;
    //   }).catch(() => {
    //     this.searchLoading = false;
    //   })
    // },

    //获取供应商等级列表
    // getSupplierGradeList() {
    //   supplierGradeList().then((res) => {
    //     this.gradeList = res;
    //   })
    // },

  },
};
</script>

<style scoped lang="scss">
    .supplier-table{
      border-left:1px solid #EBEEF5 ;
      border-top:1px solid #EBEEF5;
      font-size: 12px;
      color:#909399;
      td,th{
        border-right:1px solid #EBEEF5 ;
        border-bottom:1px solid #DCDFE6;
        text-align: center;
      }
      td{
        padding:5px;
      }
      th{
        background: #f5f7fa;
        font-weight: bold;
        padding:10px 5px;
      }
    }
    .spanBtn{
      display: inline-block;
      padding: 0px 10px;
      border: 1px solid #dcdfe6;
      color: #606266;
      border-radius: 14px;
      margin-right: 10px;
      cursor: pointer;
    }
    .spanBtn.chooseSpan{
      color:#fff;
      background: #409EFF;
      border:1px solid #409EFF;
    }
    .avatar-uploader{
        margin-bottom:10px;
        width:80px;
        height:80px;
        border:1px solid #DCDFE6;
        text-align: center;
        line-height: 80px;
    }
</style>
